%%%-------------------------------------------------------------------
%%% @author Administrator
%%% @copyright (C) 2020, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 13. 1月 2020 14:22
%%%-------------------------------------------------------------------
-module(netWebSup).
-author("Administrator").

-behaviour(supervisor).

-include("../include/logger.hrl").

-define(CLIENT_SUP, netWebServerSup).
-define(MAX_RESTART,    5).
-define(MAX_TIME,      60).



%% API
-export([start_link/0,start_child/1]).

%% Supervisor callbacks
-export([init/1]).

-define(SERVER, ?MODULE).

%%%===================================================================
%%% API functions
%%%===================================================================

%%--------------------------------------------------------------------
%% @doc
%% Starts the supervisor
%%
%% @end
%%--------------------------------------------------------------------
-spec(start_link() ->
  {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link() ->
  supervisor:start_link({local, ?SERVER}, ?MODULE, []).

%%%===================================================================
%%% Supervisor callbacks
%%%===================================================================

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Whenever a supervisor is started using supervisor:start_link/[2,3],
%% this function is called by the new process to find out about
%% restart strategy, maximum restart frequency and child
%% specifications.
%%
%% @end
%%--------------------------------------------------------------------
-spec(init(Args :: term()) ->
  {ok, {SupFlags :: {RestartStrategy :: supervisor:strategy(),
    MaxR :: non_neg_integer(), MaxT :: non_neg_integer()},
    [ChildSpec :: supervisor:child_spec()]
  }} |
  ignore |
  {error, Reason :: term()}).
init([]) ->
  NetListener = {netWebSup,                         		    % Id       = internal id
    {netWebListener, start_link, [netWebServer]},	% StartFun = {M, F, A}
    permanent,                               		% Restart  = permanent | transient | temporary
    2000,                                    		% Shutdown = brutal_kill | int() >= 0 | infinity
    worker,                                  		% Type     = worker | supervisor
    [netWebListener]                           	% Modules  = [Module] | dynamic
  },

  NetClientSupervisor = {?CLIENT_SUP,
    {supervisor, start_link, [{local, ?CLIENT_SUP}, ?MODULE, [netWebServer]]},
    permanent,
    infinity,
    supervisor,
    [netWebServer]
  },
  {ok,
    {{one_for_one, ?MAX_RESTART, ?MAX_TIME},
      [NetClientSupervisor,NetListener]
    }
  };

init([Module]) ->
  {ok,
    {_SupFlags = {simple_one_for_one, 0, 1},
      [
        % net Client
        {   undefined,                               % Id       = internal id
          {Module, start_link, []},                % StartFun = {M, F, A}
          temporary,                               % Restart  = permanent | transient | temporary (不会重启)
          2000,                                    % Shutdown = brutal_kill | int() >= 0 | infinity
          worker,                                  % Type     = worker | supervisor
          []                                       % Modules  = [Module] | dynamic
        }
      ]
    }
  }.

%%%===================================================================
%%% Internal functions
%%%===================================================================
%% 启动socket连接处理进程
start_child(Socket) ->
  supervisor:start_child(?CLIENT_SUP, [Socket]).